﻿using System;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Text;

namespace CloudFranceTriple.Models
{

    public partial class cloudfrAboq2RsDMEntities : DbContext
    {
        /// <summary>
        /// Intercepter SaveChanges pour avoir une gestion d'erreur centralisée qui gère DbEntityValidationException
        /// </summary>
        /// <returns></returns>
        public override int SaveChanges()
        {
            try
            {
                return base.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var _result = new StringBuilder();
                _result = _result.AppendLine(String.Format("{0} <br /> <br />", dbEx.Message));

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        System.Diagnostics.Trace.TraceInformation("Class: {0}, Property: {1}, Error: {2}",
                            validationErrors.Entry.Entity.GetType().FullName,
                            validationError.PropertyName,
                            validationError.ErrorMessage);

                        _result.AppendLine(String.Format("Class: {0}, Property: {1}, Error: {2}",
                            validationErrors.Entry.Entity.GetType().FullName,
                            validationError.PropertyName,
                            validationError.ErrorMessage));
                    }
                }

                throw new ApplicationException(_result.ToString(), dbEx);
            }
        }
    }
}
